package com.longfor.dragonshard.config;

import com.longfor.daenerys.config.LFProperties;
import com.longfor.drogon.common.mybatis.LFMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

import java.util.Map;

/**
 *
 * @author chi.zhang
 * @create 2018/1/11 上午9:50
 *
 * @version v1.0
 **/
@Configuration
@AutoConfigureAfter(MybatisConfiguration.class)
public class MapperConfiguration implements EnvironmentAware {

    private String basePackage;

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(Environment environment){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage(basePackage);
        mapperScannerConfigurer.setMarkerInterface(LFMapper.class);
        return mapperScannerConfigurer;
    }

    @Override
    public void setEnvironment(Environment env) {
        Map<String, Object> rpr = new RelaxedPropertyResolver(env, "longfor.mybatis")
                .getSubProperties(".");
        basePackage = rpr.get("base-package").toString();
    }
}